package com.cmict.data.infra.controller.admin.rdbcolumn;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.common.pojo.CommonResult;
import static com.cmict.data.framework.common.pojo.CommonResult.success;

import com.cmict.data.framework.excel.core.util.ExcelUtils;

import com.cmict.data.framework.operatelog.core.annotations.OperateLog;
import static com.cmict.data.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.cmict.data.infra.controller.admin.rdbcolumn.vo.*;
import com.cmict.data.infra.dal.dataobject.rdbcolumn.RdbColumnDO;
import com.cmict.data.infra.convert.rdbcolumn.RdbColumnConvert;
import com.cmict.data.infra.service.rdbcolumn.RdbColumnService;

@Api(tags = "管理后台 - 常用字段")
@RestController
@RequestMapping("/infra/rdb-column")
@Validated
public class RdbColumnController {

    @Resource
    private RdbColumnService rdbColumnService;

    @PostMapping("/create")
    @ApiOperation("创建常用字段")
    @PreAuthorize("@ss.hasPermission('sys:rdb-column:create')")
    public CommonResult<Long> createRdbColumn(@Valid @RequestBody RdbColumnCreateReqVO createReqVO) {
        return success(rdbColumnService.createRdbColumn(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新常用字段")
    @PreAuthorize("@ss.hasPermission('sys:rdb-column:update')")
    public CommonResult<Boolean> updateRdbColumn(@Valid @RequestBody RdbColumnUpdateReqVO updateReqVO) {
        rdbColumnService.updateRdbColumn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除常用字段")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:rdb-column:delete')")
    public CommonResult<Boolean> deleteRdbColumn(@RequestParam("id") Long id) {
        rdbColumnService.deleteRdbColumn(id);
        return success(true);
    }

    @DeleteMapping("/batchDelete")
    @ApiOperation("批量删除常用字段")
    @PreAuthorize("@ss.hasPermission('sys:rdb-column:delete')")
    public CommonResult<Boolean> batchDeleteRdbColumn(@RequestBody Collection<Long> ids) {
        rdbColumnService.batchDeleteRdbColumn(ids);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得常用字段")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:rdb-column:query')")
    public CommonResult<RdbColumnRespVO> getRdbColumn(@RequestParam("id") Long id) {
        RdbColumnDO rdbColumn = rdbColumnService.getRdbColumn(id);
        return success(RdbColumnConvert.INSTANCE.convert(rdbColumn));
    }

    @GetMapping("/list")
    @ApiOperation("获得常用字段列表")
    @PreAuthorize("@ss.hasPermission('sys:rdb-column:query')")
    public CommonResult<List<RdbColumnRespVO>> getRdbColumnList(@RequestParam("keyword") String keyword) {
        List<RdbColumnDO> list = rdbColumnService.getRdbColumnList(keyword);
        return success(RdbColumnConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得常用字段分页")
    @PreAuthorize("@ss.hasPermission('sys:rdb-column:query')")
    public CommonResult<PageResult<RdbColumnRespVO>> getRdbColumnPage(@Valid RdbColumnPageReqVO pageVO) {
        PageResult<RdbColumnRespVO> pageResult = rdbColumnService.getRdbColumnPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出常用字段 Excel")
    @PreAuthorize("@ss.hasPermission('sys:rdb-column:export')")
    @OperateLog(type = EXPORT)
    public void exportRdbColumnExcel(@Valid RdbColumnExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<RdbColumnDO> list = rdbColumnService.getRdbColumnList(exportReqVO);
        // 导出 Excel
        List<RdbColumnExcelVO> datas = RdbColumnConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "常用字段.xls", "数据", RdbColumnExcelVO.class, datas);
    }

}
